#!/bin/bash

# Slackware build script for citra

# Copyright 2022-2023 Nathaniel Russell <naterussell83@gmail.com>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=citra
SRCNAM=$PRGNAM-nightly-nightly
PRGVER=${PRGVER:-"1863"}
VERSION=${VERSION:-0.1863}
BOOST=${BOOST:-80a171a179c1f901e4f8dfc8962417f44865ceec}
CATCH=${CATCH:-dc001fa935d71b4b77f263fce405c9dbdfcbfe28}
CPPJWT=${CPPJWT:-e12ef06218596b52d9b5d6e1639484866a8e7067}
CRYPTOPP=${CRYPTOPP:-511806c0eba8ba5b5cedd4b4a814e96df92864a6}
CRYPTOCMAKE=${CRYPTOCMAKE:-15798ac9c2611d5c7f9ba832e2c9159bdd8945f2}
CUBEB=${CUBEB:-dc511c6b3597b6384d28949285b9289e009830ea}
DISCORDRPC=${DISCORDRPC:-963aa9f3e5ce81a4682c6ca3d136cddda614db33}
DYNARMIC=${DYNARMIC:-9af4b970d302389829448a30608c7cb4fce9b662}
ENET=${ENET:-4f8e9bdc4ce6d1f61a6274b0e557065a38190952}
EXTSOUNDTOUCH=${EXTSOUNDTOUCH:-060181eaf273180d3a7e87349895bd0cb6ccbf4a}
FMTLIB=${FMTLIB:-a33701196adfad74917046096bf5a2aa0ab0bb50}
GOOGLETEST=${GOOGLETEST:-800f5422ac9d9e0ad59cd860a2ef3a679588acb4}
INIH=${INIH:-5e1d9e2625842dddb3f9c086a50f22e4f45dfc2b}
LIBRESSL=${LIBRESSL:-8929f818fd748fd31a34fec7c04558399e13014a}
LIBUSB=${LIBUSB:-e782eeb2514266f6738e242cdcb18e3ae1ed06fa}
LIBYUV=${LIBYUV:-6900494d90ae095d44405cd4cc3f346971fa69c9}
LODEPNG=${LODEPNG:-18964554bc769255401942e0e6dfd09f2fab2093}
NIHSTRO=${NIHSTRO:-fd69de1a1b960ec296cc67d32257b0f9e2d89ac6}
SANITIZERSCMAKE=${SANITIZERSCMAKE:-aab6948fa863bc1cbe5d0850bc46b9ef02ed4c1a}
SDL2=${SDL2:-a1d1946dcba6509f0679f507b57e7b228d32e6f8}
TEAKRA=${TEAKRA:-01db7cdd00aabcce559a8dddce8798dabb71949b}
XBYAK=${XBYAK:-a1ac3750f9a639b5a6c6d6c7da4259b8d6790989}
ZSTD=${ZSTD:-e47e674cd09583ff0503f0f6defd6d23d8b718d3}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}
DEPS=$TMP/$SRCNAM-$PRGVER/externals

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$PRGVER
tar xvf $CWD/$SRCNAM-$PRGVER.tar.?z

# citra has quite a few external dependencies, we need to extract them to the correct location.
tar xvf $CWD/ext-boost-$BOOST.tar.gz -C \
  $DEPS/boost --strip-components 1
tar xvf $CWD/Catch2-$CATCH.tar.gz -C \
  $DEPS/catch2 --strip-components 1
tar xvf $CWD/cpp-jwt-$CPPJWT.tar.gz -C \
  $DEPS/cpp-jwt --strip-components 1
tar xvf $CWD/cryptopp-$CRYPTOPP.tar.gz -C \
  $DEPS/cryptopp --strip-components 1
tar xvf $CWD/cryptopp-cmake-$CRYPTOCMAKE.tar.gz -C \
  $DEPS/cryptopp-cmake --strip-components 1  
tar xvf $CWD/cubeb-$CUBEB.tar.gz -C \
  $DEPS/cubeb --strip-components 1
tar xvf $CWD/googletest-$GOOGLETEST.tar.gz -C \
  $DEPS/cubeb/googletest --strip-components 1
tar xvf $CWD/discord-rpc-$DISCORDRPC.tar.gz -C \
  $DEPS/discord-rpc --strip-components 1
tar xvf $CWD/dynarmic-$DYNARMIC.tar.gz -C \
  $DEPS/dynarmic --strip-components 1
tar xvf $CWD/enet-$ENET.tar.gz -C \
  $DEPS/enet --strip-components 1
tar xvf $CWD/ext-soundtouch-$EXTSOUNDTOUCH.tar.gz -C \
  $DEPS/soundtouch --strip-components 1
tar xvf $CWD/fmt-$FMTLIB.tar.gz -C \
  $DEPS/fmt --strip-components 1
tar xvf $CWD/inih-$INIH.tar.gz -C \
  $DEPS/inih/inih --strip-components 1
tar xvf $CWD/ext-libressl-portable-$LIBRESSL.tar.gz -C \
  $DEPS/libressl --strip-components 1
tar xvf $CWD/libusb-$LIBUSB.tar.gz -C \
  $DEPS/libusb/libusb --strip-components 1
tar xvf $CWD/libyuv-$LIBYUV.tar.gz -C \
  $DEPS/libyuv --strip-components 1
tar xvf $CWD/lodepng-$LODEPNG.tar.gz -C \
  $DEPS/lodepng/lodepng --strip-components 1
tar xvf $CWD/nihstro-$NIHSTRO.tar.gz -C \
  $DEPS/nihstro --strip-components 1
tar xvf $CWD/sanitizers-cmake-$SANITIZERSCMAKE.tar.gz -C \
  $DEPS/cubeb/cmake/sanitizers-cmake --strip-components 1
tar xvf $CWD/SDL-$SDL2.tar.gz -C \
  $DEPS/sdl2/SDL --strip-components 1
tar xvf $CWD/teakra-$TEAKRA.tar.gz -C \
  $DEPS/teakra --strip-components 1
tar xvf $CWD/xbyak-$XBYAK.tar.gz -C \
  $DEPS/xbyak --strip-components 1
tar xvf $CWD/zstd-$ZSTD.tar.gz -C \
  $DEPS/zstd --strip-components 1

# citra checks for external components by checking for their .git files.
# Since these are not included when we downloaded the commit tarballs, we'll create them.
echo "gitdir: ../../.git/modules/boost" > $DEPS/boost/.git
echo "gitdir: ../../.git/modules/catch2" > $DEPS/catch2/.git
echo "gitdir: ../../.git/modules/cpp-jwt" > $DEPS/cpp-jwt/.git
echo "gitdir: ../../.git/modules/cryptopp" > $DEPS/cryptopp/.git
echo "gitdir: ../../.git/modules/cryptopp-cmake" > $DEPS/cryptopp-cmake/.git
echo "gitdir: ../../.git/modules/cubeb" > $DEPS/cubeb/.git
echo "gitdir: ../../.git/modules/discord-rpc" > $DEPS/discord-rpc/.git
echo "gitdir: ../../.git/modules/dynarmic" > $DEPS/dynarmic/.git
echo "gitdir: ../../.git/modules/enet" > $DEPS/enet/.git
echo "gitdir: ../../.git/modules/soundtouch" > $DEPS/soundtouch/.git
echo "gitdir: ../../.git/modules/fmt" > $DEPS/fmt/.git
echo "gitdir: ../../../.git/modules/inih" > $DEPS/inih/inih/.git
echo "gitdir: ../../.git/modules/libressl" > $DEPS/libressl/.git
echo "gitdir: ../../../.git/modules/libusb" > $DEPS/libusb/libusb/.git
echo "gitdir: ../../.git/modules/libyuv" > $DEPS/libyuv/.git
echo "gitdir: ../../../.git/modules/lodepng" > $DEPS/lodepng/lodepng/.git
echo "gitdir: ../../.git/modules/nihstro" > $DEPS/nihstro/.git 
echo "gitdir: ../../.git/modules/sdl2/SDL" > $DEPS/sdl2/SDL/.git
echo "gitdir: ../../.git/modules/teakra" > $DEPS/teakra/.git
echo "gitdir: ../../.git/modules/xbyak" > $DEPS/xbyak/.git
echo "gitdir: ../../.git/modules/zstd" > $DEPS/zstd/.git 

cd $SRCNAM-$PRGVER
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

mkdir -p build
cd build
  cmake \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DLIB_SUFFIX=${LIBDIRSUFFIX} \
    -DMAN_INSTALL_DIR=/usr/man \
    -DCMAKE_BUILD_TYPE=Release ..
  make
  make install/strip DESTDIR=$PKG
cd ..

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

mv $PKG/usr/share/man $PKG/usr
find $PKG/usr/man -type f -exec gzip -9 {} \;
for i in $( find $PKG/usr/man -type l ) ; do ln -s $( readlink $i ).gz $i.gz  ; rm $i ; done

# Install hicolor
for size in $(find /usr/share/icons/hicolor -type d -mindepth 1 -maxdepth 1 -exec basename {} \;); do
    if [ -d assets/unix-icons/hicolor/${size} ]; then
        cp -a assets/unix-icons/hicolor/${size} $PKG/usr/share/icons/hicolor/${size}
    fi
done

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  CONTRIBUTING* README* license.txt \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/qt-config.ini > $PKG/usr/doc/$PRGNAM-$VERSION/qt-config.ini.sample
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
